﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;
using DevComponents.DotNetBar;


namespace QLNhanKhau
{
    public partial class frmKhaiSinh : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmKhaiSinh()
        {
            InitializeComponent();
        }

        private void frmKhaiSinh_Load(object sender, EventArgs e)
        {
           
            //load mank
            NhanKhauPublic nk = NhanKhauBLL.LoadMaNK();
ThietLapPublic tl = ThietLapBLL.LoadThietLap();

            //tao code
            //var mank = nk.MaNK + 1;
            //string chuoimank = mank.ToString() ;
            //while (chuoimank.Length < 6)
            //{
            //    chuoimank = "0" + chuoimank;
            //}
            //string MaT=tl.MaT.ToString();
            //string MaH=tl.MaQH.ToString();
            //string MaX=tl.MaPX.ToString();
            //if (tl.MaT < 10)
            //    MaT = "0" + tl.MaT.ToString();
            //if (tl.MaQH < 10)
            //    MaH = "0" + tl.MaQH.ToString();
            //if (tl.MaPX < 10)
            //    MaX = "0" + tl.MaPX.ToString();
            //txtMaNK.Text = MaT+MaH+MaX+chuoimank;

            //tao code lun
            
            int ma = nk.MaNK + 1;
            string chuoi = ma.ToString();
            if (tl.MaPX < 10)
                chuoi = "0" + ma.ToString();
            txtMaNK.Text = chuoi;
            txtMaNK.Enabled = false;
            txtMaKS.Text = chuoi;
            txtMaKS.Enabled = false;
            //load dân tộc
            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load dân tộc cha
            cmbDanTocCha.DataSource = DanTocBLL.LoadDanToc();
            cmbDanTocCha.DisplayMember = "TenDanToc";
            cmbDanTocCha.ValueMember = "MaDT";
            //load quốc tịch cha
            cmbQuocTichCha.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTichCha.DisplayMember = "TenQuocTich";
            cmbQuocTichCha.ValueMember = "MaQT";
            //load dantoc  me
            cmbDanTocMe.DataSource = DanTocBLL.LoadDanToc();
            cmbDanTocMe.DisplayMember = "TenDanToc";
            cmbDanTocMe.ValueMember = "MaDT";
            //load quoctich me
            cmbQuocTichMe.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTichMe.DisplayMember = "TenQuocTich";
            cmbQuocTichMe.ValueMember = "MaQT";
            //load tôn giáo
            //cmbTonGiao.DataSource = TonGiaoBLL.LoadTonGiao();
            //cmbTonGiao.ValueMember = "MaTG";
            //cmbTonGiao.DisplayMember = "TenTonGiao";
            //load sổ hộ khẩu
            //dgvSoHoKhau.DataSource = SoHoKhauBLL.LoadSoHoKhau();
            //dgvSoHoKhau.Columns[0].HeaderText = "Mã SHK";
            //dgvSoHoKhau.Columns[1].HeaderText = "Số nhà";
            //dgvSoHoKhau.Columns[2].HeaderText = "Tên đường";
            //dgvSoHoKhau.Columns[3].HeaderText = "KP/Ấp";
            //dgvSoHoKhau.Columns[4].Visible = false;

            //dgvSoHoKhau.Columns[0].Width = 59;
            //dgvSoHoKhau.Columns[1].Width = 50;
            //dgvSoHoKhau.Columns[2].Width = 156;
            //dgvSoHoKhau.Columns[3].Width = 50;
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result =  MessageBox.Show( "Noi dung", "Tieu de", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            int MaSHK = int.Parse(txtMaSHK.Text);
            List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(MaSHK);

            if (list_nk.Count == 0)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;
            txtChuHo.Text = list_nk.Find(item => item.QuanHe == "Chủ hộ").HoTen;
            //txtChuHo.Text = list_nk.Where(item => item.QuanHe == "Chủ hộ").FirstOrDefault().HoTen;

            dgvNhanKhau.DataSource = list_nk;
            //dgvNhanKhau.Columns[0].Visible = false;
            //dgvNhanKhau.Columns[1].Visible = false;
            //dgvNhanKhau.Columns[2].Visible = false;
            //dgvNhanKhau.Columns[3].Visible = false;
            //dgvNhanKhau.Columns[4].Visible = false;
            //dgvNhanKhau.Columns[5].Visible = false;
            //dgvNhanKhau.Columns[6].Visible = false;
            //dgvNhanKhau.Columns[7].Visible = false;
            ////dgvNhanKhau.Columns[8].Visible = false;
            //dgvNhanKhau.Columns[9].Visible = false;
            //dgvNhanKhau.Columns[10].Visible = false;
            //dgvNhanKhau.Columns[11].Visible = false;
            //dgvNhanKhau.Columns[12].Visible = false;
            //dgvNhanKhau.Columns[13].Visible = false;
            //dgvNhanKhau.Columns[14].Visible = false;
            dgvNhanKhau.Columns[15].Visible = false;
            //dgvNhanKhau.Columns[16].Visible = false;

            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauByMaSHK(MaSHK);

            txtDuong.Text = shk.Duong;
            txtAp.Text = ""+shk.KhuPho+"";
            txtSoNha.Text = shk.SoNha;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            //them khai sinh
            KhaiSinhPublic ks = new KhaiSinhPublic();
            ks.MaKS=int.Parse(txtMaKS.Text);
            ks.SoKhaiSinh =int.Parse(txtSoKhaiSinh.Text);
            ks.NgayCap = DateTime.Parse(dateNgayCap.Text);
            ks.NoiCap=txtNoiCap.Text;
            ks.NguoiDangki = txtNguoiDangki.Text;
            ks.QuanHeKS = txtQuanHeKS.Text;
            ks.NguoiCap = txtCanBo.Text;
            ks.ChucVu = txtCanBo.Text;
            ks.NoiSinh = txtNoiSinh.Text;
            ks.HoTenCha = txtHoTenCha.Text;
            ks.DanTocCha = (byte)cmbDanTocCha.SelectedValue;
            ks.QuocTichCha = (byte)cmbQuocTichCha.SelectedValue;
            ks.HoTenMe = txtHoTenMe.Text;
            ks.QuocTichMe = (byte)cmbQuocTichMe.SelectedValue;
            ks.DanTocMe = (byte)cmbQuocTichMe.SelectedValue;
            KhaiSinhBLL.InsertKhaiSinh(ks);

            //thêm nhân khẩu
            NhanKhauPublic nk = new NhanKhauPublic();
            nk.MaNK = int.Parse(txtMaNK.Text);
            nk.HoTen = txtHoTen.Text;
            nk.TenGoiKhac = "";
            //nk.CMND = 0;
            nk.MaKS =int.Parse(txtMaKS.Text);
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaDT = (byte)cmbQuocTich.SelectedValue;
            nk.QuanHe = txtQuanHeChuHo.Text;
            nk.NgheNghiep = "";
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            nk.MaCT = 0;
            nk.MaTG = 14;
            nk.MaTD = 1;
            nk.CMND = "";
            nk.TenGoiKhac = "";
            nk.MaSHK = int.Parse(txtMaSHK.Text);
            
            NhanKhauBLL.InsertNhanKhauKS(nk);
            //load lai form
            frmKhaiSinh_Load(sender, e);
            btnTim_Click(sender, e);
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;

        }

        private void txtMaSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoKhaiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        
    }
}
